﻿using Microsoft.Extensions.DependencyInjection;
using PetaPoco;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.DBContext;
using PmSoft.Repositories;
using PmSoft.Utilities;


namespace Modules.Logs;

/// <summary>
/// 各应用log4net调试日志仓储
/// </summary>
[UseDI(ServiceLifetime.Scoped)]
public class Log4netDebugRepository : Repository<Log4netDebug, LogDbContext>
{
    public Log4netDebugRepository(LogDbContext dbContext, IApplicationContext applicationContext) : base(dbContext, applicationContext)
    {
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="query"></param>
    /// <param name="pageIndex"></param>
    /// <param name="pageSize"></param>
    /// <returns></returns>
    public IPagedList<Log4netDebug> GetLog4netDebugs(Log4netQuery query, int pageIndex, int pageSize)
    {
        Sql sql = Sql.Builder;
        sql.Select("id")
            .From("log4net_debug");
        //这里根据自己的查询参数判断 
        if (!string.IsNullOrEmpty(query.Keywords))
            sql.Where("(app_name Like @0 or logger like @0 or message like @0) ", "%" + StringUtility.StripSQLInjection(query.Keywords) + "%");
        if (query.StartTime.HasValue)
            sql.Where("date>=@0", query.StartTime.Value);
        sql.OrderBy("id DESC");
        return GetPagingEntities(pageSize, pageIndex, sql);
    }
}